// Supports Windows, Linux, Mac, and Raspberry Pi

package main

import (
	//"encoding/xml"
	//"strings"
	"fmt"
	"github.com/facchinm/go-serial"
	//"io/ioutil"
	//"log"
	//"os"
	//"regexp"
)

type OsSerialPort struct {
	Port  string
	State int
}

func SerialListGet() []OsSerialPort {
	arrPorts := []OsSerialPort{}

	ports, err := serial.GetPortsList()
	if err != nil {
		fmt.Printf("error %v \n", err)
	}
	if len(ports) == 0 {
		fmt.Printf("No serial ports found!")
	}
	for _, port := range ports {
		//fmt.Printf("Found port: %v\n", port)
		arrPorts = append(arrPorts, OsSerialPort{Port: port, State: serialPortCheckIsOpen(port)})
	}
	return arrPorts
}

func serialPortCheckIsOpen(port string) int {
	f, _ := serialFindPortByName(port)
	if f {
		return 1
	}
	return 0
}
